package com.song.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.song.entity.Depart;
import com.song.entity.Emp;
import com.song.service.IDepartService;
import com.song.service.IEmpService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author song
 * @since 2024-02-28
 */
@RestController
@RequestMapping("/depart")
@Schema(name = "部门控制器",description = "部门数据访问接口")
public class DepartController {

    @Resource
    private IDepartService departService;
    @Resource
    private IEmpService empService;

    @GetMapping("/")
    @Operation(description = "部门列表",method = "get请求") // OS: Operation System 操作系统
    public List<Depart> depts(){
        return this.departService.list();
    }

    @PostMapping("/")
    public boolean addDepart(@RequestBody Depart depart){
        return this.departService.save(depart);
    }

    @DeleteMapping("/{id}")
    public boolean deleteDepart(@PathVariable Integer id){
        //判断该部门下是否存在员工
        QueryWrapper<Emp>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("d_id",id);
        Long m = this.empService.count(queryWrapper);
        if(m>0){
          System.out.println("该部门下存在员工，不能删除");
          return  false;
        }else {

            return this.departService.removeById(id);
        }
    }


}
